package com.tangyuan.common.util;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class ImageDimensionUtils {

    /**
     * 获取图像的宽度和高度
     *
     * @param file MultipartFile 图像文件
     * @return 包含图像宽度和高度的整型数组，第一个元素为宽度，第二个元素为高度
     * @throws IOException 如果无法读取图像文件或者图像文件格式不支持，抛出 IOException
     */
    public static int[] getImageDimensions(MultipartFile file) throws IOException {
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image != null) {
                int width = image.getWidth();
                int height = image.getHeight();
                return new int[]{width, height};
            } else {
                throw new IOException("Failed to read image dimensions.");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
